import requests

from common.config import getconf
from tests.new_rypark_background.get_cookie import internal_token


class RentCar:
    # =====================长租车=====================

    # 可选长租规则列表查询
    def query_active_rent_rule_list(self, env, rentType="1"):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-order-server/rule/rent/getActiveList?rentType=" + rentType
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        return requests.get(url, headers=headers).json()

    # 可选车库列表查询
    def query_child_garage_list(self, env):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/parkingLot/" + getconf(env, "parking_lot_id") + "/childGarageInfo"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        return requests.get(url, headers=headers).json()

    # 新建长租车辆
    def create_rent_car(self, env, parkingGarageIdList=[],phone="",spaceType=1,spaceNo="",
                    rentId=-1, rentBegin="", rentEnd="", orderFee=0,
                    discountFee=0, plate="", rentType=1, partitionStart="",partitionEnd="",includeHoliday=True,weekLimit=""):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-order-server/v1/rent/order/createOrder"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
          "parkingGarageIdList": parkingGarageIdList,
          "phone": phone,
          "spaceType": spaceType,
          "spaceNo": spaceNo,
          "rentId": rentId,
          "rentBegin": rentBegin,
          "rentEnd": rentEnd,
          "orderFee": orderFee,
          "discountFee": discountFee,
          "parkingLotId": getconf(env, "parking_lot_id"),
          "plate": plate,
          "rentType": rentType,
          "partitionStart": partitionStart,
          "partitionEnd": partitionEnd,
          "includeHoliday": includeHoliday,
          "weekLimit": weekLimit
        }
        return requests.post(url, headers=headers, json=dict).json()

    # 长租车辆支付
    def pay_car_order(self, env, orderNo="",payChannel=3):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-order-server/v1/rent/order/payOrder"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "orderNo": orderNo,
            "payChannel": 3 #支付方式 3 现金
        }
        return requests.post(url, headers=headers, json=dict).json()

    # 长租车辆列表查询
    def query_rent_car_list(self, env, plate="",status="", rentType=1):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCar/list/page"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
          "parkingLotId": int(getconf(env, "parking_lot_id")),
          "rentType": rentType
        }
        if plate:
            dict["plate"]= plate
        if status:
            dict["status"]= status
        return requests.post(url, headers=headers,json=dict).json()

    # 停用长租车辆
    def disable_rent_car(self, env, dict):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCar/status/disable"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        data = {
            "id": dict['id'],
            "status": 5
        }
        return requests.post(url, headers=headers, json=data).json()

    # 启用长租车辆
    def enable_rent_car(self, env, dict):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCar/status/enable"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        data = {
            "id": dict['id'],
            "status": 1
        }
        return requests.post(url, headers=headers, json=data).json()

    # 长租车辆详情列表查询
    def query_rent_car_detail_list(self, env, plate="", rentType=1):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCarDetail/list/page"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "orderNo": plate,
            "rentalCarId": "NORMAL",
            "rentType": rentType
        }
        return requests.get(url, headers=headers, json=dict).json()

    # 长租车辆详情查询
    def query_rent_car_detail(self, env, id=""):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCarDetail/detail/"+id
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        return requests.get(url, headers=headers, json=dict).json()


    # 详情长租车辆编辑
    def update_rent_car(self, env, dict ,mobile,parkingGarageIds,stallType=1):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCarDetail/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "id": dict['id'],
            "mobile": mobile,
            "parkingGarageIds": parkingGarageIds,
            "stallNo": "自动化测试编辑长租车辆",
            "stallType": stallType,
            "rentalCarId": dict['rentalCarId'],
            "parkingLotId": dict['parkingLotId']
        }
        return requests.post(url, headers=headers, json=dict).json()

    # 详情停用长租车辆
    def detail_disable_rent_car(self, env, dict):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCarDetail/status/disable"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "id": dict['id'],
            "rentalCarId": dict['rentalCarId'],
            "status": 5
        }
        return requests.post(url, headers=headers, json=dict).json()

    # 详情启用长租车辆
    def detail_enable_rent_car(self, env, dict):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/rentalCarDetail/status/enable"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "id": dict['id'],
            "rentalCarId": dict['rentalCarId'],
            "status": 1
        }
        return requests.post(url, headers=headers, json=dict).json()